package com.ys.peony.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yklab.library.refreshlayout.BGANormalRefreshViewHolder;
import com.yklab.library.refreshlayout.BGARefreshLayout;
import com.ys.peony.R;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.interfaces.OnContractClickListener;
import com.ys.peony.model.domain.ContractSortModel;
import com.ys.peony.ui.adapter.ContractSortAdapter;
import com.ys.peony.ui.view.DialogContract;
import com.ys.peony.ui.view.IdCardDialog;
import com.ys.peony.utils.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class ContractSortListActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate, OnContractClickListener
        , IdCardDialog.MyClickListener {
    @BindView(R.id.rl_refresh)
    BGARefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_qy)
    TextView tv_qy;
    @BindView(R.id.titleback)
    RelativeLayout titleback;
    @BindView(R.id.title)
    TextView title;

    private List<ContractSortModel.FileTemplateBean> mList;
    private LinearLayoutManager mLayoutManager;
    private ContractSortAdapter mAdapter;
    private String loanId = "";
    private ContractSortModel mContractSortModel;

    @Override
    protected int getContentResId() {
        return R.layout.activity_contract_sort_list;
    }

    @Override
    protected void initView() {
        initListener();
        initAdapter();
        initData();
    }

    private void initListener() {
        tv_qy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(loanId);
            }
        });
        titleback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initAdapter() {
        mList = new ArrayList<>();
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRefreshLayout.setDelegate(this);
        // 设置下拉刷新和上拉加载更多的风格;参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
        mRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(this, true));
        mAdapter = new ContractSortAdapter(this, mList, this);
        recyclerview.setHasFixedSize(true);
        recyclerview.setLayoutManager(mLayoutManager);
        recyclerview.setAdapter(mAdapter);

    }

    private void initData() {
        Intent intent = getIntent();
        String data = "";
        String name = "";
        if (intent != null && intent.getExtras() != null) {
            Bundle bundle = intent.getExtras();
            loanId = bundle.getString(YSConstant.PARAM, "");
            data = bundle.getString("data", "");
            name = bundle.getString("name", "");
        }
        if (!TextUtils.isEmpty(data)) {
            mContractSortModel = JSONUtil.parseObject(data, ContractSortModel.class);
            if (!"1".equals(mContractSortModel.getFlag_sign())) {
                tv_qy.setVisibility(View.GONE);
            }
            mList.clear();
            mList.addAll(mContractSortModel.getFileTemplate());
            mAdapter.setData(mList);
        } else {
            mRefreshLayout.beginRefreshing();
        }
        title.setText(name + "合同列表");
    }

    private void getData() {
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("loan_id", loanId);
        httpManagerUtils.get(HttpURL.contractList, params, new BaseRespCallBack(this) {
            @Override
            public void onRespSuccess(String respStr) {
                mRefreshLayout.endRefreshing();
                ContractSortModel contractSortModel = JSONUtil.parseObject(respStr, ContractSortModel.class);
                if (contractSortModel != null && contractSortModel.getFileTemplate() != null) {
                    mContractSortModel = contractSortModel;
                    if (!"1".equals(mContractSortModel.getFlag_sign())) {
                        tv_qy.setVisibility(View.GONE);
                    }
                    mList.clear();
                    mList.addAll(contractSortModel.getFileTemplate());
                    mAdapter.setData(mList);
                }
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                mRefreshLayout.endRefreshing();
                //showBlankView();
            }
        });
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        getData();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

    @Override
    public void onContractClick(int parentpos, int childpos) {
        Bundle bundle = new Bundle();
        String url = HttpURL.CONTRACT_DOWN + "/" + loanId + "/" + mList.get(parentpos).getBusiFileTemplateList().get(childpos).getFile_template_name();
        bundle.putString(YSConstant.PARAM, url);
        skipToTargetPage(ContractDetailActivity.class, bundle);

    }

    /**
     * 业务id
     */
    private void showDialog(final String loanId) {
        DialogContract dialog = new DialogContract(this);
        String phone = mContractSortModel.getBusiClient().getMobile();
        dialog.setData(loanId, phone);
        dialog.setListener(new DialogContract.OnContracyListener() {
            @Override
            public void success() {
                mRefreshLayout.beginRefreshing();
            }
        });
    }

    @Override
    public void onClick(String name, String idCard) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
